package com.zzu.base.controller.permission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzu.base.common.pojo.ResultResponse;
import com.zzu.base.controller.permission.vo.PermissionPageReqVO;
import com.zzu.base.entity.PermissionDO;
import com.zzu.base.service.PermissionService;
import com.zzu.base.controller.permission.vo.PermissionAddReqVO;
import com.zzu.base.controller.permission.vo.PermissionGetRespVO;
import com.zzu.base.controller.permission.vo.PermissionUpdateReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/permission")
@Tag(name = "权限")
@PreAuthorize("hasPermission(null,'restricted_use')")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    private static final CopyOptions COPY_OPTIONS = CopyOptions.create()
            .setIgnoreNullValue(false)
            .setIgnoreError(true);

    @PreAuthorize("hasPermission(null,'permit_all')")
    @Operation(method = "权限新增")
    @PostMapping("/add")
    public ResultResponse<Boolean> permissionAdd(@Valid @RequestBody PermissionAddReqVO permissionAddReqVO) {
        PermissionDO permissionDO = BeanUtil.toBean(permissionAddReqVO, PermissionDO.class, COPY_OPTIONS);
        return ResultResponse.success(permissionService.save(permissionDO));
    }

    @PreAuthorize("hasPermission(null,'permit_all')")
    @Operation(method = "权限移除")
    @GetMapping("/remove")
    public ResultResponse<Boolean> permissionRemove(@Positive Long id) {
        return ResultResponse.success(permissionService.removePermission(id));
    }

    @Operation(method = "权限列表")
    @GetMapping("/list")
    public ResultResponse<List<PermissionGetRespVO>> permissionList() {
        List<PermissionDO> permissionList = permissionService.list();
        List<PermissionGetRespVO> resultList = permissionList.stream()
                .map(permissionDO -> BeanUtil.toBean(permissionDO, PermissionGetRespVO.class, COPY_OPTIONS))
                .collect(Collectors.toList());
        return ResultResponse.success(resultList);
    }

    @Operation(method = "权限获取")
    @GetMapping("/get")
    public ResultResponse<PermissionGetRespVO> permissionGet(@Positive Long id) {
        PermissionDO permissionDO = permissionService.getById(id);
        PermissionGetRespVO respVO = BeanUtil.toBean(permissionDO, PermissionGetRespVO.class, COPY_OPTIONS);
        return ResultResponse.success(respVO);
    }

    @PreAuthorize("hasPermission(null,'permit_all')")
    @Operation(method = "权限修改")
    @PostMapping("/update")
    public ResultResponse<Boolean> permissionUpdate(@Valid @RequestBody PermissionUpdateReqVO permissionUpdateReqVO) {
        PermissionDO permissionDO = BeanUtil.toBean(permissionUpdateReqVO, PermissionDO.class, COPY_OPTIONS);
        return ResultResponse.success(permissionService.update(permissionDO));
    }

    @Operation(method = "权限分页")
    @PostMapping("/page")
    public ResultResponse<IPage<PermissionGetRespVO>> permissionPage(@Valid @RequestBody PermissionPageReqVO permissionPageReqVO) {
        IPage<PermissionDO> permissionPage = permissionService.getPermissionPage(permissionPageReqVO);

        IPage<PermissionGetRespVO> resultPage = new Page<>();
        BeanUtil.copyProperties(permissionPage, resultPage, COPY_OPTIONS);
        List<PermissionGetRespVO> records = permissionPage.getRecords().stream()
                .map(permissionDO -> BeanUtil.toBean(permissionDO, PermissionGetRespVO.class, COPY_OPTIONS))
                .collect(Collectors.toList());
        resultPage.setRecords(records);

        return ResultResponse.success(resultPage);
    }
}